/*-------------------------------------------------------------------------------------------------------------------------------------------
Program: cdf_amt_sent_v2
----------------------------------------------------------------------------------------------------------------------------------------*/
*version Stata/SE 17.0

clear all
set more off
macro drop _all

cd "/Volumes/GoogleDrive/My Drive/Documents/JMP/Work/Tables/tables_replication" 

use 				"merged_v0.31_replication.dta" , clear

cd "Figure2_cdf_amt_sent_v2"

encode session_id, gen(sess_id)
xtset sess_id


label define 		treatment_l		0 "No Rule in Round 2" 					1 "Info 40 & No Rule in Round 2"      		///
									2 "Info 40 & Rule 60 in Round 2" 					3 "Info 40 & Rule 100 in Round 2"		 	   			   		///
									4 "Info 60 & No Rule in Round 2" 					5 "Info 60 & Rule 60 in Round 2"				///
									6 "Info 60 & Rule 100 in Round 2" 
label values		treatment		treatment_l


***************************************************************************************
*CDF OF AMOUNT SENT IN PART I AND PART II IN CONTROL/KOLMOGOROV-SMIRNOV TEST/MANN-WHITNEY TEST/T-TEST
***************************************************************************************
forvalues i = 1(1)6 {
preserve
local v : label (treatment) `i'
display "`i' is `v'"


/*Generate a cumulative distribution for amt_sent1 for N40R0*/
cumul amt_sent1 if treatment == `i', gen(c_amt1) eq
cumul amt_sent2 if treatment == `i', gen(c_amt2) eq

/*Stack command stacks c_amt1 and c_amt2, stacks amt_sent1 and amt_sent2*/
stack c_amt1 amt_sent1 c_amt2 amt_sent2 if treatment==`i', into (c amt) wide clear



sort c_amt1 c_amt2
/*Mann-Whitney test shows that */

ranksum amt, by(_stack)

/*Two-sample Kolmogorov–Smirnov test*/

ksmirnov amt, by(_stack) exact
local p = round(r(p_exact), 0.001)

/*The following command draws the cdf of amt_sent1 and amt_sent2 in the N40R0 group. 
*/
/*ttest is the parametric test. There is no difference in means between round 1 and round 2*/
ttest amt, by(_stack)

if `i'!=3 {
#delimit ;


line c_amt1 c_amt2 amt, xla(#10) xsize(7) ysize(5) 
				xtitle("Amount Shared by Dictators", size(medium))
				ytitle("CDF" , size(medium))
				yscale(titlegap(3))
				xlabel(0(20)200)
				xscale(range(0 200))
				legend(label(1 "Round 1")) ///
				legend(label(2 "Round 2")) ///
				subtitle("`v'", size(med))
				xlabel( ,angle(45)) 		
				scheme(s2manual)
				text(0.1 140 "p = `p' in a KS test" "for equality of distributions" "in Round 1 and Round 2")
				;
				 graph save "cdf`i'.gph", replace;
#delimit cr
}
else  {
	#delimit ;


line c_amt1 c_amt2 amt, xla(#10) xsize(7) ysize(5) 
				xtitle("Amount Shared by Dictators", size(medium))
				ytitle("CDF" , size(medium))
				yscale(titlegap(3))
				xlabel(0(20)200)
				xscale(range(0 200))
				legend(label(1 "Round 1")) ///
				legend(label(2 "Round 2")) ///
				subtitle("`v'", size(med))
				xlabel( ,angle(45)) 		
				scheme(s2manual)
				text(0.1 140 "p<0.001 in a KS test" "for equality of distributions" "in Round 1 and Round 2")
				;
				 graph save "cdf`i'.gph", replace;
#delimit cr
}

/*Data is restored*/
restore 
}

gr combine 		cdf1.gph 	cdf2.gph 	cdf3.gph, ///
				ycommon 		xcommon	cols(1)	ysize(11) xsize(6)	///
				title("Info 40") 
graph save "cdf123_v2.gph", replace
				 
								*				
graph export amtsent12info40_v2.pdf, replace 

gr combine 		cdf4.gph 	cdf5.gph 	cdf6.gph, ///
				ycommon 		xcommon	cols(1)	ysize(11) xsize(6)	///
				title("Info 60") 			 
graph save "cdf456_v2.gph", replace
							*				
graph export amtsent12info60_v2.pdf, replace 


gr combine		cdf123_v2.gph cdf456_v2.gph, cols(2) ysize(11) xsize(6) ///
				title("Cumulative Distribution Functions (CDF)" "of high-caste participants' sharing in" " Rounds 1 and 2 by treatment conditions") 
graph save "cdf123456_v2.gph", replace
graph export amtsent12_v2.pdf, replace 


